$(function() {
    //页面初始化设置
    // FastClick.attach(document.body);
    //隐藏顶栏
    hideFix('.am-header');

    var option = {
        URL: pageData.URL + '/account/login',
        val: {
            email: '[name="email"]',
            _xsrf: '[name="_xsrf"]',
            password: '[name="password"]'
        },
        defaultVal: {
            openid: pageData.openid
        },
        captcha: true
    }
    next_url = getUrlParam("next");
    if(next_url){
        option.defaultVal.next = next_url;
    }
    simpleForm('#J_LoginForm', option, '#J_LoginBtn',
        function(status) {
            //onSubmit
            if(!status) {
                toast($('.form-error:first-child').data('validation-message'));
            }
        },
        function() {
            //onValid
        },
        function(group, msg) {
            //onInValid
        },
        function(res) {
            //callback
            if(res.status === true) {
                window.location.href = res.message.url;
            } else {
                if(res.errors.is_redirect != undefined){
                    window.location.href = res.errors.url;
                }
                toast(res.errors.msg);
            }
        }
    );
    clearForm('[name="email"]');
    clearForm('[name="password"]');
});

//获取url中的参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
